﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;
using System.Collections.ObjectModel;
using Portal.PorletData.Utility;

namespace Portal.Systems.Render
{
    public class PageConfigRenderBll
    {
        public static void RegisterAjaxPro(string SiteId, System.Web.UI.Page Page)
        {
            string PageId = (string)WebEnvironments.Request(SiteId, "PageId");
            PageZoneModuleCls[] PageZoneModules = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingModulesInPage(SiteId, PageId);
            for (int iIndex = 0; iIndex < PageZoneModules.Length; iIndex++)
            {
                IPorletData PorletData = PorletDataUtility.Find(SiteId, PageZoneModules[iIndex].ModuleId);
                if (PorletData != null)
                {
                    PorletData.RegisterAjaxPro(SiteId, Page);
                }
            }
        }


        public static string GetPlugInConfigJavascript(string SiteId,string PageId)
        {
            string JavascriptText = "";
            PageZoneModuleCls[] PageZoneModules = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingModulesInPage(SiteId, PageId);
            for (int iIndex = 0; iIndex < PageZoneModules.Length; iIndex++)
            {
                IPorletData PorletData = PorletDataUtility.Find(SiteId, PageZoneModules[iIndex].ModuleId);
                if (PorletData != null)
                {
                    JavascriptText = JavascriptText + PorletData.GetJavascript(SiteId) + "\r\n";
                }
            }
            return JavascriptText;
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string PageId = (string)WebEnvironments.Request(SiteId, "PageId");
                string ServiceId = (string)WebEnvironments.Request(SiteId, "ServiceId");
                if (string.IsNullOrEmpty(PageId))
                {
                    throw new Exception("Tham số trang không hợp lệ!");
                }
                if (string.IsNullOrEmpty(ServiceId)) ServiceId = "";
                PageCls OPage = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreatePageModel(SiteId, PageId);
                string Html =
                    GetPlugInConfigJavascript(SiteId,PageId)+
                   "<script language=javascript>\r\n" +
                   "    function ClientSavePageInfo(PageId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        SaveZoneIndex='';\r\n"+
                   "        MaxZone=parseInt(document.getElementById('txtMaxZone').value);\r\n"+
                   "        for(var iIndexZone=0;iIndexZone<MaxZone;iIndexZone++)\r\n"+
                   "        {\r\n"+
                   "            PageZoneId = document.getElementById('txtPageZoneId'+iIndexZone).value;\r\n" +
                   "            Max=parseInt(document.getElementById('txtMaxInZone'+PageZoneId).value);\r\n"+
                   "            for(var iIndex=0;iIndex<Max;iIndex++)\r\n"+
                   "            {\r\n"+
                   "                Ids='txtPageZoneModuleId'+PageZoneId+iIndex;\r\n" +
                   "                PageZoneModuleId=document.getElementById(Ids).value;\r\n" +
                   "                SortIndex=document.getElementById('txtSortIndex'+PageZoneModuleId).value;\r\n"+
                   "                CssClass=document.getElementById('txtCssClass'+PageZoneModuleId).value;\r\n" +
                   "                SaveZoneIndex=SaveZoneIndex+PageZoneModuleId+':'+SortIndex+':'+CssClass+';';\r\n" +
                   "            }\r\n"+
                   "        }\r\n"+
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideSavePageInfo(SiteId, PageId, SaveZoneIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divRightContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "        alert(AjaxOut.InfoMessage);\r\n" +
                   "    }\r\n" +

                   "    function ClientSaveModuleInZone(PageId, PageZoneModuleId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        ServiceId=document.getElementById('drpSelectPlugInView'+PageZoneModuleId).value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideSavePageModuleInZone(SiteId, PageId, PageZoneModuleId, ServiceId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "    }\r\n"+

                   "    function ClientAddShortCut(PageId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideAddShortCut(SiteId, PageId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        alert(AjaxOut.InfoMessage);\r\n" +
                   "    }\r\n" +

                   "    function ClientPermissionPorlet(LibBookId, LibBookCollectionItemId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSidePermisionPorlet(SiteId, LibBookId, LibBookCollectionItemId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        window.open(AjaxOut.RetUrl,'_self');\r\n" +
                   "    }\r\n" +

                   "    function ClientOpenService()\r\n"+
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        PageId=document.getElementById('txtPageId').value;\r\n"+
                   "        ServiceId=document.getElementById('drpSelect').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideReadingAvailable(SiteId, PageId, ServiceId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListServiceContent').innerHTML=AjaxOut.HtmlContent;\r\n"+
                   "    }\r\n"+

                   "    function ClientAddModuleToZone(PageId, ServiceId, ModuleId)\r\n"+
                   "    {\r\n"+
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        PageZoneId=document.getElementById('drpSelectPageZone').value;\r\n" +
                   "        ExtraCss = document.getElementById('txtExtraCss').value;\r\n"+
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.AddModuleToZone(SiteId, PageId, ServiceId, ModuleId, PageZoneId, ExtraCss).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divRightContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+

                   "    function ClientRemoveModule(PageId,PageZoneModuleId)\r\n"+
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa module khỏi vùng chưa?')==false)return;\r\n"+
                   "        PageId=document.getElementById('txtPageId').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.RemoveModuleFromZone(SiteId, PageId, PageZoneModuleId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divRightContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+

                   "    function ClientMoveModuleZone(PageId,PageZoneModuleId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        PageId=document.getElementById('txtPageId').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideDrawModuleFromZone(SiteId, PageId, PageZoneModuleId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divMoveZone').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divPorletForm').style.display='none';\r\n" +
                   "        document.getElementById('divMoveZone').style.display='block';\r\n" +
                   "    }\r\n"+

                   "    function ClientConfigModuleZone(PageZoneModuleId, PlugInWebPartId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideDrawConfigForm(SiteId, PageZoneModuleId, PlugInWebPartId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divMoveZone').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divPorletForm').style.display='none';\r\n" +
                   "        document.getElementById('divMoveZone').style.display='block';\r\n" +
                   "    }\r\n"+


                   "    function ClientBackFromMoveZone()\r\n"+
                   "    {\r\n"+
                   "        document.getElementById('divMoveZone').innerHTML='';\r\n" +
                   "        document.getElementById('divPorletForm').style.display='block';\r\n" +
                   "        document.getElementById('divMoveZone').style.display='none';\r\n" +
                   "    }\r\n"+

                   "    function ClientBackFromConfig()\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('divMoveZone').innerHTML='';\r\n" +
                   "        document.getElementById('divPorletForm').style.display='block';\r\n" +
                   "        document.getElementById('divMoveZone').style.display='none';\r\n" +
                   "    }\r\n" +

                   "    function ClientSaveMoveZone(PageId,PageZoneModuleId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        PageZoneId=document.getElementById('drpSelectPageZone').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideSaveMoveZone(SiteId, PageId, PageZoneModuleId, PageZoneId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        ClientBackFromMoveZone();\r\n"+
                   "        document.getElementById('divRightContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+


                   "    function ClientSyncPage(PageId)\r\n"+
                   "    {\r\n"+
                   "        if(confirm('Bạn đã chắc chắn đồng bộ trang lên web chưa?')==false)return;\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageConfigRenderBll.ServerSideSyncPage(SiteId, PageId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        alert(AjaxOut.InfoMessage);\r\n" +
                   "    }\r\n"+
                   "</script>\r\n" +

                   " <div id=divPermissionForm></div>\r\n"+
                   " <input type=hidden id=\"txtPageId\" value=\""+PageId+"\">\r\n"+
                   " <div id=divMoveZone></div>\r\n"+
                   " <div id=divPorletForm>\r\n" +
                   "    <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddShortCut('" + PageId + "');\"><img class=image src=\"" + IconRoot + "/ShortCut.png\"><div>Tạo shortcut</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientSyncPage('" + PageId + "');\"><img class=image src=\"" + IconRoot + "/Sync.png\"><div>Đồng bộ lên công</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientSavePageInfo('" + PageId + "');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('PageMan.aspx','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "    </div>\r\n" +
                   "<h3>Thiết lập cấu hình trang web - "+OPage.PageTitle+"</h3>\r\n" +
                   "<div id=divMainForm>\r\n" +
                        ServerSideDrawModuleInZone(SiteId, PageId, ServiceId).HtmlContent +
                   "</div>\r\n" +
                   
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        public static AjaxOut ServerSideDrawModuleInZone(
            string SiteId, 
            string PageId,
            string ServiceId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                PageZoneCls[] PageZones = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingPageZone(SiteId, PageId, null);
                string SelectPageZone = "<select id=drpSelectPageZone class=drplist>\r\n";
                for (int iIndex = 0; iIndex < PageZones.Length; iIndex++)
                {
                    SelectPageZone += "   <option value=\"" + PageZones[iIndex].PageZoneId + "\">" + PageZones[iIndex].ZoneName + "</option>\r\n";
                }
                SelectPageZone += "</select>\r\n";
                string Html =
                    //" <div class=CssFilter>Kéo thả thiết lập các module trong vùng</div>\r\n" +
                    //" <div class=CssFilter>\r\n" +
                    //"   <div class=RowItem>Lọc theo loại block: " + SelectBlockType + "</div>\r\n" +
                    //" </div>\r\n" +
                    "<table class=CssTableZone>\r\n" +
                    "   <tr>\r\n" +
                    "       <td class=c1>\r\n" +
                    "           <div class=CssListService>\r\n"+
                    "               <div>Chọn vùng: </div>\r\n"+
                                        SelectPageZone+
                    "               <div>Nhập Css mở rộng</div>\r\n"+
                    "               <div><input id=\"txtExtraCss\" value=\"\" type=textbox class=textbox></div>\r\n" +
                    "               <div class=CssFilterService id=\"divListServiceContent\">\r\n" +
                                        ServerSideReadingAvailable(SiteId, PageId, ServiceId).HtmlContent +
                    "               </div>\r\n"+
                    
                    "           </div>\r\n"+
                    "       </td>\r\n" +
                    "       <td class=c2>\r\n"+
                    "       <div id=divRightContent>\r\n"+
                    ServerSideReadingRightContent(SiteId, PageId).HtmlContent +
                    "       </div>\r\n"+
                    " </td>\r\n" +
                    "</tr>\r\n" +
                    "</table>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingRightContent(
            string SiteId,
            string PageId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                PageZoneCls[] PageZones = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingPageZone(SiteId, PageId, null);
                string Html = "<input type=\"hidden\" id=\"txtMaxZone\" value=\""+PageZones.Length+"\">";
                for (int ZoneIndex = 0; ZoneIndex < PageZones.Length; ZoneIndex++)
                {
                    Html +=
                        " <div class=ZoneCol>\r\n" +
                        "  <div class=\"title\">" + PageZones[ZoneIndex].ZoneName + "</div>\r\n" +
                        "   <div class=\"column\" id=Col1> \r\n" +
                                ServerSideReadingForZone(SiteId, PageId, PageZones[ZoneIndex].PageZoneId, ZoneIndex).HtmlContent +
                        "   </div> \r\n" +
                        " </div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
            
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingForZone(
            string SiteId,
            string PageId,
            string PageZoneId,
            int    ZoneIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                PageZoneModuleCls[] PageZoneModules = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingPageZoneModule(SiteId, PageZoneId);
                string Html =
                    "<input type=\"hidden\" id=\"txtPageZoneId" + ZoneIndex + "\" value=\"" + PageZoneId + "\">\r\n" +
                    "<input type=\"hidden\" id=\"txtMaxInZone"+PageZoneId+"\" value=\"" + PageZoneModules.Length + "\">";

                if (PageZoneModules.Length == 0)
                {
                    Html += "<div class=CssNoData>Chưa có module nào trong vùng</div>\r\n";
                }
                else
                {
                    Html +=
                        "<div class=tabcontent>\r\n" +
                        "<table>\r\n";
                    for (int iIndex = 0; iIndex < PageZoneModules.Length; iIndex++)
                    {
                        string PageZoneModuleId = PageZoneModules[iIndex].PageZoneModuleId;
                        string PorletHeader = PageZoneModules[iIndex].ModuleTitle;

                        Collection<IPluginView>
                            PlugInViews = PlugInViewUtility.Find(SiteId, PageZoneModules[iIndex].ServiceId);
                        string SelectPlugInViewText =
                            "<select id=\"drpSelectPlugInView" + PageZoneModules[iIndex].PageZoneModuleId + "\" onchange=\"javascript:ClientSaveModuleInZone('" + PageZoneModules[iIndex].PageId+"','"+ PageZoneModules[iIndex].PageZoneModuleId + "');\">" +
                            "   <option value=\"\">Chọn dịch vụ hiển thị</option>\r\n";
                        for (int iIndexPlugInView = 0; iIndexPlugInView < PlugInViews.Count; iIndexPlugInView++)
                        {
                            if (PlugInViews[iIndexPlugInView].ServiceId.Equals(PageZoneModules[iIndex].PlugInViewId))
                            {
                                SelectPlugInViewText += " <option selected value=\"" + PlugInViews[iIndexPlugInView].ServiceId + "\">" + PlugInViews[iIndexPlugInView].ServiceName + "</option>\r\n";
                            }
                            else
                            {
                                SelectPlugInViewText += " <option value=\"" + PlugInViews[iIndexPlugInView].ServiceId + "\">" + PlugInViews[iIndexPlugInView].ServiceName + "</option>\r\n";
                            }
                        }
                        SelectPlugInViewText += "</select>\r\n";
                        if (PlugInViews.Count == 0)
                        {
                            SelectPlugInViewText = "<div class=CssNoPluginView>(Chưa có dịch vụ hiển thị)</div>\r\n";
                        }

                        string CssClass = PageZoneModules[iIndex].CssClass;

                        Html +=
                            "<tr>\r\n" +
                            "    <td class=tdIndex><input type=hidden id=\"txtPageZoneModuleId" + PageZoneId + iIndex.ToString() + "\" value=\"" + PageZoneModuleId + "\"><input id=\"txtSortIndex" + PageZoneModuleId + "\" style=\"width:20px;text-align:right;\" value=\"" + (iIndex + 1) + "\"></td>\r\n" +
                            "    <td>" + PorletHeader + "<div class=CssPlugInView>" + SelectPlugInViewText + "</div><div>Mã thẻ Css đại diện</div><div><input id=\"txtCssClass" + PageZoneModuleId + "\" value=\"" + CssClass + "\"></div></td>\r\n" +
                            "    <td class=tdButton><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientRemoveModule('" + PageId + "','" + PageZoneModuleId + "');\"></td>\r\n" +
                            "    <td class=tdButton><input type=button class=button value=\"Chuyển\" onclick=\"javascript:ClientMoveModuleZone('" + PageId + "','" + PageZoneModuleId + "');\"></td>\r\n" +
                            "    <td class=tdButton><input type=button class=button value=\"Cấu hình\" onclick=\"javascript:ClientConfigModuleZone('" +PageZoneModules[iIndex].PageZoneModuleId+"','"+ PageZoneModules[iIndex].ModuleId + "');\"></td>\r\n" +
                            "</tr>\r\n";
                    }
                    Html +=
                        "</table>\r\n" +
                        "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut AddModuleToZone(
            string SiteId, 
            string PageId, 
            string ServiceId,
            string ModuleId, 
            string PageZoneId,
            string ExtraCss)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                PorletDataItemCls OPorletDataItem = PorletDataUtility.CreatePorletDataItem(SiteId, ServiceId, ModuleId);
                if (OPorletDataItem == null) throw new Exception("Không tìm thấy dữ liệu này!");
                PageZoneModuleCls OPageZoneModule = new PageZoneModuleCls();
                OPageZoneModule.ModuleId = ModuleId;
                OPageZoneModule.PageId = PageId;
                OPageZoneModule.ModuleTitle = OPorletDataItem.Subject;
                OPageZoneModule.PageZoneId = PageZoneId;
                OPageZoneModule.SortIndex = 0;
                OPageZoneModule.Visible = 1;
                OPageZoneModule.ServiceId = ServiceId;
                OPageZoneModule.CssClass = ExtraCss;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddPageZoneModule(SiteId, OPageZoneModule);
                OAjaxOut.HtmlContent = ServerSideReadingRightContent(SiteId, PageId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut RemoveModuleFromZone(string SiteId, string PageId, string PageZoneModuleId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeletePageZoneModule(SiteId, PageZoneModuleId);
                OAjaxOut.HtmlContent = ServerSideReadingRightContent(SiteId, PageId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDrawModuleFromZone(string SiteId, string PageId, string PageZoneModuleId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                PageZoneCls[] PageZones = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingPageZone(SiteId, PageId, null);
                string SelectPageZone = "<select id=\"drpSelectPageZone\">\r\n";
                for (int iIndex = 0; iIndex < PageZones.Length; iIndex++)
                {
                    SelectPageZone += "   <option value=\"" + PageZones[iIndex].PageZoneId + "\">" + PageZones[iIndex].ZoneName + "</option>\r\n";
                }
                SelectPageZone += "</select>\r\n";
                SiteParam OSiteParam=WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                OAjaxOut.HtmlContent =
                   "    <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientSaveMoveZone('" + PageId + "','"+PageZoneModuleId+"');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBackFromMoveZone();\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "    </div>\r\n" +
                    "<div class=CssFormSaveUpdate>\r\n" +
                    "   <table>\r\n" +
                    "       <tr>\r\n" +
                    "           <td class=Col1>Chọn vùng cần chuyển</td>\r\n" +
                    "           <td class=Col2>" + SelectPageZone + "</td>\r\n" +
                    "       </tr>\r\n" +
                    "   </table>\r\n" +
                    "</div>\r\n";
                    
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDrawConfigForm(string SiteId, string PageZoneModuleId, string PlugInWebPartId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                IPorletData PorletData = PorletDataUtility.Find(SiteId, PlugInWebPartId);
                if (PorletData == null)
                {
                    throw new Exception("Không cung cấp cấu hình thiết lập cho module này");
                }
                if (PorletData.SupportConfig)
                {
                    RenderInfoCls ORenderInfo=new RenderInfoCls();
                    AjaxOut OAjaxOutDrawConfig = PorletData.DrawConfigForm(SiteId, PageZoneModuleId, ORenderInfo);
                    if (OAjaxOutDrawConfig == null)
                    {
                        throw new Exception("Không cung cấp cấu hình thiết lập cho module này");
                    }
                    OAjaxOut.HtmlContent = OAjaxOutDrawConfig.HtmlContent;
                }
                else
                {
                    throw new Exception("Không cung cấp cấu hình thiết lập cho module này");
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingAvailable(
            string SiteId,
            string PageId,
            string ServiceId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                IPorletData[]
                    PorletDatas = PorletDataUtility.LoadPlugIn(SiteId);
                if (PorletDatas.Length > 0 && string.IsNullOrEmpty(ServiceId))
                {
                    ServiceId = PorletDatas[0].ServiceId;
                }
                string SelectData = 
                    "<div>Chọn dịch vụ</div>\r\n"+
                    "<select id=\"drpSelect\" type=drplist class=drplist onchange=javascript:ClientOpenService();>\r\n";
                for (int iIndex = 0; iIndex < PorletDatas.Length; iIndex++)
                {
                    if (ServiceId.Equals(PorletDatas[iIndex].ServiceId))
                    {
                        SelectData += "   <option selected value=\"" + PorletDatas[iIndex].ServiceId + "\">" + PorletDatas[iIndex].ServiceName + "</option>\r\n";
                    }
                    else
                    {
                        SelectData += "   <option value=\"" + PorletDatas[iIndex].ServiceId + "\">" + PorletDatas[iIndex].ServiceName + "</option>\r\n";
                    }
                }
                SelectData += "</select>\r\n";
                Html += SelectData;


                IPorletData OPorletData = PorletDataUtility.Find(SiteId, ServiceId);
                Collection<PorletDataItemCls> ColPorletDataItems = new Collection<PorletDataItemCls> { };
                if (OPorletData != null)
                {
                    ColPorletDataItems = OPorletData.GetPorletDataItems(SiteId);
                }
                if (ColPorletDataItems.Count == 0)
                {
                    Html += "<div class=CssNoData>Không có dữ liệu cho dịch vụ này</div>\r\n";
                }
                else
                {
                    Html +=
                        "<div class=tabcontent>\r\n" +
                        "<table>\r\n";
                    for (int iIndex = 0; iIndex < ColPorletDataItems.Count; iIndex++)
                    {
                      
                        Html +=
                            "  <tr> \r\n" +
                            "    <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                            "    <td class=tdButton><input value=\"Đưa vào\" type=button class=button onclick=\"javascript:ClientAddModuleToZone('" +PageId+"','"+ServiceId+"','"+ ColPorletDataItems[iIndex].Id + "');\"></td> \r\n" +
                            "    <td>" + ColPorletDataItems[iIndex].Subject + "</td> \r\n" +
                            "  </tr> \r\n";
                    }
                    Html += "</table>\r\n" +
                          "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSidePermisionPorlet(
            string SiteId,
            string LibBookId,
            string LibBookCollectionItemId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                //BackSessionCls OBackSession = new BackSessionCls();
                //OBackSession.BackSessionId = System.Guid.NewGuid().ToString();
                //OBackSession.SessionUserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                //OBackSession.Url = "admin/modules/library/ManageLibBook.aspx?id=" + LibBookId;
                //SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddBackSession(SiteId, OBackSession);
                //OAjaxOut.RetUrl =
                //    "../Admin/Permission.aspx?ptc=" + LibBlockPermission.StaticId + "&id=" + LibBookCollectionItemId + "&backsessionid=" + OBackSession.BackSessionId;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAddShortCut(string SiteId, string PageId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                //SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, LibDatabasePermission.StaticId, LibDatabasePermission.funcManage, DatabaseId, UserId, true);
                PageCls OPage = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreatePageModel(SiteId, PageId);
                UserQuickLinkCls OUserQuickLink = new UserQuickLinkCls();
                OUserQuickLink.frkUserId = UserId;
                OUserQuickLink.LinkType = "WebPage";
                OUserQuickLink.SortIndex = 0;
                OUserQuickLink.Subject = "Trang: " + OPage.PageTitle;
                OUserQuickLink.ObjectId = PageId;
                OUserQuickLink.LinkUrl = "modules/admin/PageConfig.aspx?PageId="+PageId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddUserQuickLink(SiteId, OUserQuickLink);
                OAjaxOut.InfoMessage = "Tạo shortcut ra màn hình chính thành công!";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = "Đã có shortcut này không tạo thêm nữa";
                OAjaxOut.HtmlContent = "Đã có shortcut này không tạo thêm nữa";
                ex.Source = "";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSavePageModuleInZone(
            string SiteId, 
            string PageId,
            string PageZoneModuleId,
            string PlugInViewId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SavePageZoneModuleView(SiteId, PageZoneModuleId, PlugInViewId);
                OAjaxOut.InfoMessage = "Đã thiết lập cấu hình hiển thị thành công";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                ex.Source = "";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSyncPage(
            string SiteId, 
            string PageId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                PageCls OPage = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreatePageModel(SiteId, PageId);
                string XmlData = PageParser.GetXml(OPage);
                WebSyncServiceUtility.CreateSyncWs(SiteId).ProcessCommand("xyz1234",SiteId,"SyncPage", XmlData);
                OAjaxOut.InfoMessage = "Đã đồng bộ lên máy chủ cổng thành công";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                ex.Source = "";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSaveMoveZone(string SiteId, string PageId, string PageZoneModuleId, string PageZoneId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SavePageZoneForMove(SiteId, PageZoneModuleId, PageZoneId);
                OAjaxOut.InfoMessage = "Chuyển vùng thành công";
                OAjaxOut.HtmlContent = ServerSideReadingRightContent(SiteId, PageId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                ex.Source = "";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSavePageInfo(
            string SiteId, 
            string PageId, 
            string SortIndexText)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string[] Items = SortIndexText.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                PageZoneModuleCls[] 
                    PageZoneModules = new PageZoneModuleCls[Items.Length];
                for (int iIndex = 0; iIndex < Items.Length; iIndex++)
                {
                    PageZoneModules[iIndex] = new PageZoneModuleCls();
                    string[] Ids=Items[iIndex].Split(new char[]{':'},StringSplitOptions.RemoveEmptyEntries);
                    PageZoneModules[iIndex].PageZoneModuleId = Ids[0];
                    PageZoneModules[iIndex].SortIndex = int.Parse(Ids[1]);
                    if (Ids.Length == 3)
                    {
                        PageZoneModules[iIndex].CssClass = Ids[2];
                    }
                }
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SavePageZoneModuleInfo(SiteId, PageZoneModules);
                OAjaxOut.HtmlContent = ServerSideReadingRightContent(SiteId, PageId).HtmlContent;
                OAjaxOut.InfoMessage = "Cập nhật trang thành công";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                ex.Source = "";
            }

            return OAjaxOut;
        }
    }
}

